﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKAreaStatistic.Utils;
using YJKGeometryFuncs;
using YJKRevitView;

namespace YJKAreaStatistic.Manage
{
	internal class AreaPlanManager
	{
		public static bool SelectFloors(Autodesk.Revit.DB.Document doc, ref List<ViewPlan> selectFloors)
		{
			List<Level> levels = FloorSelectManager.GetSortLevels(doc, double.MinValue, double.MaxValue);
			List<ViewPlan> list = new List<ViewPlan>();
			List<double> list2 = new List<double>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(ViewPlan)))
			{
				ViewPlan viewPlan = element as ViewPlan;
				if (viewPlan != null && (int)viewPlan.ViewType == 1 && viewPlan.GenLevel != null)
				{
					list2.Add(viewPlan.GenLevel.Elevation);
					list.Add(viewPlan);
				}
			}
			int i;
            Predicate<double> ss9__0 = null;
			int j;
			for (i = 0; i < levels.Count; i = j)
			{
				List<double> list3 = list2;
				Predicate<double> match;
				if ((match = ss9__0) == null)
				{
					match = (ss9__0 = ((double item) => Geometry.IsEqual(item, levels[i].Elevation)));
				}
				int num = list3.FindIndex(match);
				if (num == -1)
				{
					levels.RemoveAt(i);
					j = i - 1;
					i = j;
				}
				else
				{
					selectFloors.Add(list[num]);
				}
				j = i + 1;
			}
			return true;
		}

		public static bool GetArchAreaPlane(Autodesk.Revit.DB.Document doc, ref List<ViewPlan> lstOverAreaPlan, bool bIsOverground = false)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			if (!AreaPlanManager.GetAllAreaPlan(doc, ref list))
			{
				return false;
			}
			foreach (ViewPlan viewPlan in list)
			{
				if (viewPlan.Title.Contains(AreaPlanManager.m_strArchAreamName))
				{
					if (bIsOverground && Geometry.Greaterthan_Or_Equal(viewPlan.Origin.Z, 0.0))
					{
						lstOverAreaPlan.Add(viewPlan);
					}
					else if (!bIsOverground && Geometry.LessThan(viewPlan.Origin.Z, 0.0))
					{
						lstOverAreaPlan.Add(viewPlan);
					}
				}
			}
			return true;
		}

		public static bool GetAllArchAreaPlane(Autodesk.Revit.DB.Document doc, ref List<ViewPlan> lstAreaPlan)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			if (!AreaPlanManager.GetAllAreaPlan(doc, ref list))
			{
				return false;
			}
			foreach (ViewPlan viewPlan in list)
			{
				if (viewPlan.Title.Contains(AreaPlanManager.m_strArchAreamName))
				{
					lstAreaPlan.Add(viewPlan);
				}
			}
			return true;
		}

		public static bool GetAllInnerAreaPlane(Autodesk.Revit.DB.Document doc, ref List<ViewPlan> lstInnerPlan)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			if (!AreaPlanManager.GetAllAreaPlan(doc, ref list))
			{
				return false;
			}
			foreach (ViewPlan viewPlan in list)
			{
				if (viewPlan.Title.Contains(AreaPlanManager.m_strInnerAreaName))
				{
					lstInnerPlan.Add(viewPlan);
				}
			}
			return true;
		}

		public static bool IsLevelOverGround(string strLevelName, Autodesk.Revit.DB.Document doc)
		{
			try
			{
				List<ViewPlan> list = new List<ViewPlan>();
				if (!AreaPlanManager.GetArchAreaPlane(doc, ref list, true))
				{
					return false;
				}
				foreach (ViewPlan viewPlan in list)
				{
					if (strLevelName == viewPlan.GenLevel.Name)
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static bool IsLevelUnderGround(string strLevelName, Autodesk.Revit.DB.Document doc)
		{
			try
			{
				List<ViewPlan> list = new List<ViewPlan>();
				if (!AreaPlanManager.GetArchAreaPlane(doc, ref list, false))
				{
					return false;
				}
				foreach (ViewPlan viewPlan in list)
				{
					if (strLevelName == viewPlan.GenLevel.Name)
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static bool GetAllAreaPlan(Autodesk.Revit.DB.Document doc, ref List<ViewPlan> lstAreaPlan)
		{
			List<ViewPlan> elemByClassWithDocument = FilterElementByClass.GetElemByClassWithDocument<ViewPlan>(doc);
			if (elemByClassWithDocument == null && elemByClassWithDocument.Count < 1)
			{
				return false;
			}
			foreach (ViewPlan viewPlan in elemByClassWithDocument)
			{
				if (!viewPlan.IsTemplate && (int)viewPlan.ViewType == 116)
				{
					lstAreaPlan.Add(viewPlan);
				}
			}
			return true;
		}

		private static string m_strArchAreamName = "总建筑面积";

		private static string m_strInnerAreaName = "净面积";
	}
}
